<?php

namespace App\Jobs;

use App\Models\Common\Log;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

// php artisan queue:work --queue=log
// 日志记录
class LogJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $data;

    /**
     * 任务可尝试的次数
     *
     * @var int
     */
    public $tries = 1;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Execute the job.
     * $data:
     * @return void
     */
    public function handle()
    {
        //处理
        try {
            
            Log::create([
                'url' => $this->data['url'],
                'ip' => $this->data['ip'],
                'member_uuid' => $this->data['member_uuid'],
                'time' => $this->data['time'],
                'request' => $this->data['request'],
                'response' => $this->data['response'],
            ]);

        } catch (\Exception $exception) {
             $this->fail($exception);
        }
    }

    /**
     * 
     *  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'migo.laravel_failed_jobs' doesn't exist (SQL: insert into `laravel_failed_jobs` (`connection`, `queue`, `payload`, `exception`, `failed_at`) values (redis,
     **/
    public function fail($exception)
    {
    }
}


